package com.ecoolex.ying.space.core.manager;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.ying.space.core.dao.DesignerProjectItemsProductCustomDAO;
import com.ecoolex.ying.space.entity.*;
import com.ecoolex.ying.space.pojo.EditDesignerProjectItemsProductCustomDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Date;
import java.util.List;

import static com.ecoolex.ying.space.entity.DesignerProjectItemsProductCustom.*;

@Service
public class DesignerProjectItemsProductCustomManager {
    @Autowired
    private DesignerProjectItemsProductCustomDAO dao;

    void create(DesignerProjectItemsProduct itemsProduct, String createUser) {
        DesignerProjectItemsProductCustom record = new DesignerProjectItemsProductCustom();
        record.setItemsProductId(itemsProduct.getItemsProductId());
        record.setUserId(itemsProduct.getUserId());
        record.setProjectId(itemsProduct.getProjectId());
        record.setItemsId(itemsProduct.getItemsId());
        record.setPickGoodsId(itemsProduct.getPickGoodsId());
        record.setPickProductId(itemsProduct.getPickProductId());
        record.setDeleted(NOT_DELETED);
        record.setCreateUser(createUser);
        record.setCreateTime(Date.from(Instant.now()));
        record.setUpdateUser(createUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.insert(record);
    }

    void copy(Integer sourceItemsProductId, DesignerProjectItemsProduct targetItemsProduct, String createUser) {
        DesignerProjectItemsProductCustom record = getByItemsProductId(sourceItemsProductId);
        record.setItemsProductId(targetItemsProduct.getItemsProductId());
        record.setProjectId(targetItemsProduct.getProjectId());
        record.setItemsId(targetItemsProduct.getItemsId());
        record.setCreateUser(createUser);
        record.setCreateTime(Date.from(Instant.now()));
        record.setUpdateUser(createUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.insert(record);
    }

    void deleteByItemsProductId(Integer itemsProductId, String updateUser) {
        DesignerProjectItemsProductCustom record = new DesignerProjectItemsProductCustom();
        record.setDeleted(IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        DesignerProjectItemsProductCustomExample example = new DesignerProjectItemsProductCustomExample();
        example.createCriteria()
                .andItemsProductIdEqualTo(itemsProductId)
                .andDeletedEqualTo(NOT_DELETED);

        dao.updateByExampleSelective(record,
                example,
                Column.deleted,
                Column.updateUser,
                Column.updateTime);
    }

    void deleteByItemsId(DesignerProject project, Integer itemsId, String updateUser) {
        DesignerProjectItemsProductCustom record = new DesignerProjectItemsProductCustom();
        record.setDeleted(IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        DesignerProjectItemsProductCustomExample example = new DesignerProjectItemsProductCustomExample();
        example.createCriteria()
                .andItemsIdEqualTo(itemsId)
                .andDeletedEqualTo(NOT_DELETED);

        dao.updateByExampleSelective(record,
                example,
                Column.deleted,
                Column.updateUser,
                Column.updateTime);
    }

    void deleteByPickProductId(Integer pickProductId, String updateUser) {
        DesignerProjectItemsProductCustom record = new DesignerProjectItemsProductCustom();
        record.setDeleted(IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        DesignerProjectItemsProductCustomExample example = new DesignerProjectItemsProductCustomExample();
        example.createCriteria()
                .andPickProductIdEqualTo(pickProductId)
                .andDeletedEqualTo(NOT_DELETED);

        dao.updateByExampleSelective(record,
                example,
                Column.deleted,
                Column.updateUser,
                Column.updateTime);
    }

    void deleteByPickGoodsId(Integer pickGoodsId, String updateUser) {
        DesignerProjectItemsProductCustom record = new DesignerProjectItemsProductCustom();
        record.setDeleted(IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        DesignerProjectItemsProductCustomExample example = new DesignerProjectItemsProductCustomExample();
        example.createCriteria()
                .andPickGoodsIdEqualTo(pickGoodsId)
                .andDeletedEqualTo(NOT_DELETED);

        dao.updateByExampleSelective(record,
                example,
                Column.deleted,
                Column.updateUser,
                Column.updateTime);
    }

    void deleteByProjectId(Integer projectId, String updateUser) {
        DesignerProjectItemsProductCustom record = new DesignerProjectItemsProductCustom();
        record.setDeleted(IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        DesignerProjectItemsProductCustomExample example = new DesignerProjectItemsProductCustomExample();
        example.createCriteria()
                .andProjectIdEqualTo(projectId)
                .andDeletedEqualTo(NOT_DELETED);

        dao.updateByExampleSelective(record,
                example,
                Column.deleted,
                Column.updateUser,
                Column.updateTime);
    }

    public void update(Integer userId, Integer itemsProductId, EditDesignerProjectItemsProductCustomDTO edit, String updateUser) {
        // 属性拷贝
        DesignerProjectItemsProductCustom record = BeanUtil.copy(edit, new DesignerProjectItemsProductCustom());
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        DesignerProjectItemsProductCustomExample example = new DesignerProjectItemsProductCustomExample();
        example.createCriteria()
                .andItemsProductIdEqualTo(itemsProductId)
                .andUserIdEqualTo(userId)
                .andDeletedEqualTo(NOT_DELETED);

        int row = dao.updateByExampleSelective(record,
                example,
                Column.productNo,
                Column.productName,
                Column.productTexture,
                Column.productDescribe,
                Column.brandName,
                Column.positionDescr,
                Column.productNumber,
                Column.productImage,
                Column.productPrice,
                Column.remark,
                Column.updateUser,
                Column.updateTime);

        BizAssert.isTrue(row == 1, ResultCode.BIZ.build(50), "修改自定义属性失败");
    }

    public DesignerProjectItemsProductCustom getByItemsProductId(Integer itemsProductId) {
        return dao.selectByPrimaryKeyWithLogicalDelete(itemsProductId, NOT_DELETED);
    }

    public List<DesignerProjectItemsProductCustom> queryByItemsId(Integer itemsId) {
        DesignerProjectItemsProductCustomExample example = new DesignerProjectItemsProductCustomExample();
        example.createCriteria()
                .andItemsIdEqualTo(itemsId)
                .andDeletedEqualTo(NOT_DELETED);
        return dao.selectByExample(example);
    }
}